package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.OrdersMapper;
import com.itheima.domian.*;
import com.itheima.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    UserService userService;
    @Autowired
    AddressBookService addressBookService;

    @Override
    public void submit(Orders orders) {
        //分析将数据 页面传递过来的是 定单的一部分信息 将信息封装到订单 因为购物车的信息跟订单 详情信息基本上是一样的 进行对象拷贝
        Long id = BaseContext.getId();//用户id
        long id1 = IdWorker.getId();//生成订单
        BigDecimal bigDecimal = new BigDecimal(0);//总金额

        //1. 先 查询出购物车信息 将数据封装到订单详情表  并算出总金额
        List<ShoppingCart> list = shoppingCartService.list(Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, id));
        for (ShoppingCart shoppingCart : list) {
            //创建 订单详情表的对象
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id1);
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            Integer number = orderDetail.getNumber();//数量
            BigDecimal amount = orderDetail.getAmount();//价格
            bigDecimal = bigDecimal.add(new BigDecimal(number).multiply(amount));
            //添加到 订单详情表
            orderDetailService.save(orderDetail);
        }
        //2. 定单 表通过 用户表 获取到对应的信息进行封装
        //查询用户信息表
        User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getId, id));
        //查询地址信息 将数据封装点单表中
        AddressBook one1 = addressBookService.getOne(Wrappers.<AddressBook>lambdaQuery().eq(AddressBook::getUserId, id));

        orders.setId(id1);
        orders.setNumber(String.valueOf(id1));
        orders.setStatus(1);
        orders.setUserId(id);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(bigDecimal);
        orders.setUserName(one.getName());
        orders.setPhone(one1.getPhone());
        orders.setAddress(one1.getDetail());
        orders.setConsignee(one1.getConsignee());
        //3.信息都封装进去了就将购物车的数据都清空
        shoppingCartService.remove(Wrappers.lambdaQuery(ShoppingCart.class).eq(ShoppingCart::getUserId, id));
    }

    //分页查询
    @Override
    public Page<Orders> pages(Integer page, Integer pageSize, Long number, String beginTime, String endTime) {
        Page<Orders> page1 = new Page<>(page, pageSize);
        this.page(page1, Wrappers.lambdaQuery(Orders.class).eq(number != null, Orders::getNumber, number)
                .between(beginTime != null, Orders::getOrderTime, beginTime, endTime));

      /*  //创建第二个
        Page<OrdersDto> page2 = new Page<>();
        BeanUtils.copyProperties(page1, page2, "records");
        List<OrdersDto> collect = page1.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getId, orders.getUserId()));
            ordersDto.setUserName(one.getName());
            return ordersDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);
        return page2;*/
        return page1;
    }
}
